package com.test.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ThirdServlet
 */
@WebServlet("/ThirdServlet")
public class ThirdServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	private static String[] response = {"Yes", "No", "Maybe", "Later", 
		"It is your call", "Not a chance"};

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ThirdServlet()
	{
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		process(request, response);
	}

	protected void process(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		String reply = generateReply();

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<HTML><HEAD><TITLE>Decision Maker</TITLE></HEAD>");
		out.println("<BODY>");
		out.println("<H1>Your answer is:'" + reply + "'</H1>");
		out.println("</BODY></HTML>");
		
	}
	
	private String generateReply()
	{
		int respIndex = new Random().nextInt(response.length);
		return response[respIndex];
	}

}
